package com.example.demo.wbgl.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.wbgl.entity.Blog;
import com.example.demo.wbgl.entity.Users;
import com.example.demo.wbgl.service.IUsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author gongxl
 * @since 2021-06-01
 */
@Controller
@RequestMapping("/users")
public class UsersController {
    @Autowired
    private IUsersService iUsersService;
    /**
     * @Author Gxl
     * @Date 2021/6/2 9:45
     * @Return java.lang.String
     * @Exception
     * @Description 跳转到登录页
     */
    @RequestMapping("/toLogin")
    public String toLogin(Model model) {
        model.addAttribute("users",new Users());
        return "login";
    }
    /**
     * @Author Gxl
     * @Date 2021/6/2 9:45
     * @Return java.lang.String
     * @Exception
     * @Description 登录功能
     */
    @RequestMapping("/login")
    public String login(Users users,HttpSession session,Model model) {
        if(session.getAttribute("users")!=null){
            return "main";
        }else {
            Users user2 = iUsersService.getOne(new QueryWrapper<Users>().eq("username", users.getUsername())
                    .eq("password", users.getPassword()),false);
            if(user2!=null){
                session.setAttribute("users", user2);
                return "main";
            }else {
                model.addAttribute("errorInfo","用户名密码错误");
                return "login";
            }
        }
    }


    /**
     * @Author Gxl
     * @Date 2021/6/5 15:16
     * @Return java.lang.String
     * @Exception
     * @Description 上传用户头像
     */
    @RequestMapping("/addPic")
    @ResponseBody
    public String addPic(MultipartFile file) {
        //获得上传的文件名
        String fileName = file.getOriginalFilename();
        //判断有没有指定的目录
        File targetFile = new File("d:/temp", fileName);
        if (!targetFile.exists()) {
            targetFile.mkdirs();
        }
        // 上传
        try {
            file.transferTo(targetFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "/temp/"+fileName;
    }
    /**
     * @Author Gxl
     * @Date 2021/6/5 15:17
     * @Return java.lang.String
     * @Exception  添加用户
     * @Description
     */
    @RequestMapping("/addUser")
    @ResponseBody
    public Map addUser(@RequestBody Users users) {
        Map res = new HashMap();
        boolean save = iUsersService.save(users);
        if(save){
            res.put("status",200);
            res.put("msg","用户保存成功");
        }else{
            res.put("status",300);
            res.put("msg","用户保存失败");
        }
        return res;
    }

    /**
     * 分页获取用户列表
     * @param current
     * @param size
     * @return
     */
    @RequestMapping("/getUserPageList")
    @ResponseBody
    public IPage<Users> getUserPageList(@RequestParam(value = "current",defaultValue = "1")long current,
                                      @RequestParam(value = "size",defaultValue = "5")long size){
        QueryWrapper<Users> wrapper = new QueryWrapper<>();
        IPage<Users> userPage = new Page<>(current, size);
        userPage = iUsersService.page(userPage,wrapper);
        return userPage;
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @RequestMapping("/delUser")
    @ResponseBody
    public Map delUser(@RequestParam("id") long id){
        Map res = new HashMap();
        boolean save = iUsersService.removeById(id);
        if(save){
            res.put("status",200);
            res.put("msg","用户删除成功");
        }else{
            res.put("status",300);
            res.put("msg","用户删除失败");
        }
        return res;
    }

    /**
     * 添加用户时，用户名验证
     * @param username
     * @return
     */
    @RequestMapping("/checkUserName")
    @ResponseBody
    public Boolean checkUserName(@RequestParam("username") String username){
        Boolean a;
        Users users = iUsersService.getOne(new QueryWrapper<Users>().eq("username", username),false);
        if(users!=null){
            a=true;
        }else{
            a=false;
        }
        return a;
    }
}

